package com.jinlong.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinlong.common.api.BaseApiService;
import com.jinlong.constants.BaseApiConstants;
import com.jinlong.entity.UserEntity;
import com.jinlong.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

public class BaseController {

    @Autowired
    private UserFeign userFeign;
    public UserEntity getUserEntity(String token){
        Map<String, Object> userMap = userFeign.getUser(token);
        Integer code = (Integer) userMap.get(BaseApiConstants.HTTP_CODE_NAME);
        if(!code.equals(BaseApiConstants.HTTP_200_CODE)){
           return null;
        }
        LinkedHashMap dataMap = (LinkedHashMap) userMap.get(BaseApiConstants.HTTP_DATA_NAME);
        String jsonString = new JSONObject().toJSONString(dataMap);
        UserEntity userEntity = new JSONObject().parseObject(jsonString, UserEntity.class);
        return userEntity;
    }


    public String setError(HttpServletRequest request, String msg, String addres){
        request.setAttribute("error","注册失败！");
        return addres;
    }
}
